import {
  login,
  logout,
  getInfo
} from '@/api/user'
import {
  getToken,
  setToken,
  removeToken,
  setMenus,
  removeMenus,
  getMenus,
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'
import defaultSettings from '@/settings'

const getDefaultState = () => {
  return {
    token: getToken(),
    menus: getMenus(),
    userid: localStorage.getItem('user.userid') ? localStorage.getItem('user.userid') : 0,
    nickname: localStorage.getItem('user.nickname') ? localStorage.getItem('user.nickname') : '',
    avatar: localStorage.getItem('user.avatar') ? localStorage.getItem('user.avatar') : '',
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
    localStorage.setItem('user.token', token)
  },
  SET_USERID: (state, uid) => {
    state.userid = uid
    localStorage.setItem('user.userid', uid)
  },
  SET_NICKNAME: (state, nickname) => {
    state.nickname = nickname
    localStorage.setItem('user.nickname', nickname)
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
    localStorage.setItem('user.avatar', avatar)
  },
  SET_MENUS: (state, menus) => {
    state.menus = menus
    setMenus(menus)
    resetRouter()
  }
}

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    return new Promise((resolve, reject) => {
      login({...userInfo}).then(response => {
        const {
          data
        } = response
        setToken(data.token)
        commit('SET_TOKEN', data.token)
        commit('SET_NICKNAME', data.nickname)
        commit('SET_AVATAR', data.avatar)
        commit('SET_USERID', data.uid)
        commit('SET_MENUS', data.menus)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const {
          data
        } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const {
          nickname,
          avatar
        } = data

        commit('SET_USERINFO', data)
        commit('SET_NICKNAME', nickname)
        commit('SET_AVATAR', avatar)
        commit('SET_USERID', data.uid)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        removeMenus()
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
